/**
 * Project Name:javaBaseReview
 * Package Name:com.xuwei.ext.refactor.day01
 * author:David
 * Date:2016年3月20日上午9:22:30
 * Copyright (c) 2016, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.ext.refactor.day01;

import java.util.Enumeration;
import java.util.Vector;

/**
 * @author David
 * @since 2016年3月20日 上午9:22:30
 * @version 
 * @since JDK 1.6
 */
public class Rental {
	private Movie movie;
	private int daysRented;
	private Double result;

	/**
	 * 
	 */
	public Rental(Movie movie, int daysRented) {
		this.movie =movie;
		this.daysRented = daysRented;
	}
	
	public int getDaysRented(){
		return daysRented;
	}
	
	public Movie getMovie(){
		return movie;
	}
	
	public double getCharge() {
		if(result!=null){
			return result.doubleValue();
		}
        result=0D;
		switch(getMovie().getPriceCode()){
		case Movie.REGULAR:
			result+=2;
			if(getDaysRented()>2){
				result+=(getDaysRented()-2)*1.5;
			}
			break;
		case Movie.NEW_RELEASE:
			result+=getDaysRented()*3;
			break;
		case Movie.CHILDRENS:
			result+=1.5;
			if(getDaysRented()>3){
				result+=(getDaysRented()-3)*1.5;
			}
			break;
		}
		return result;
	}

}

class Customer{
	private String name;
	private Vector rentals = new Vector();
	
	public Customer(String name){
		this.name = name;
	}
	
	public void addRental(Rental rental){
		rentals.add(rental);
	}
	
	public String getName(){
		return name;
	}
	
	public String statement(){
		double totalAmount = 0;
		int frequentRenterPoints = 0;
		Enumeration rentals = this.rentals.elements();
		String result = "Rental Record for "+getName()+"\n";
		while(rentals.hasMoreElements()){
			Rental each = (Rental)rentals.nextElement();
			frequentRenterPoints++;
			if(each.getMovie().getPriceCode()==Movie.NEW_RELEASE&&each.getDaysRented()>1){
				frequentRenterPoints++;
			}
			
			result+="\t"+each.getMovie().getTitle()+"\t"+String.valueOf(each.getCharge())+"\n";
			totalAmount+=each.getCharge();
			
		}
		//add footer lines
		result+="Amount owed is "+String.valueOf(totalAmount)+"\n";
		result+="You earned "+String.valueOf(frequentRenterPoints)+" frequent renter points";
		return result;
	}

}

